package com.liujit.bms.app.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.app.domain.model.BlogArticleComment;
import com.liujit.bms.app.domain.params.BlogArticleCommentParams;
import com.liujit.bms.app.domain.query.BlogArticleCommentQuery;
import com.liujit.bms.app.domain.result.BlogArticleCommentResult;
import com.liujit.bms.app.mapper.BlogArticleCommentMapper;
import com.liujit.bms.app.service.BlogArticleCommentService;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import com.liujit.web.utils.JwtUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description 博客文章评论Service实现类
 * @Author liujun
 * @Date 2021-07-17 10:16:09
 */
@Service
public class BlogArticleCommentServiceImpl extends ServiceImpl<BlogArticleCommentMapper, BlogArticleComment>
        implements BlogArticleCommentService {

    @Override
    public PageBean<BlogArticleCommentResult> getPage(BlogArticleCommentQuery query) {
        Page<BlogArticleComment> page = Pagination.page(query);
        query.setPid(0L);
        query.setIsOpen(Convert.toShort(1));
        IPage<BlogArticleCommentResult> iPage = super.baseMapper.selPage(page, query);
        return PageBean.page(iPage);
    }

    @Override
    public Boolean add(BlogArticleCommentParams params) {
        // 添加评论量
        super.baseMapper.addCommentNum(params.getArticleId());
        BlogArticleComment entity = DozerConvertor.convertor(params, BlogArticleComment.class);
        // 添加
        entity.setPid(0L);
        entity.setMemberId(JwtUtil.getUserId());
        entity.setIsOpen(Convert.toShort(0));
        entity.setCommentTime(new Date());
        return super.save(entity);
    }

    @Override
    public BlogArticleCommentResult get(Long id) {
        BlogArticleComment blogArticleComment = super.getById(id);
        if (blogArticleComment == null) {
            throw new DataNotFoundException();
        }
        return DozerConvertor.convertor(blogArticleComment, BlogArticleCommentResult.class);
    }
}
